package ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Font;

import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Toolkit;
import java.awt.Component;
import javax.swing.ScrollPaneConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

@SuppressWarnings("serial")
public class front extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					front frame = new front();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public front() {
		//Set Windows Look&Feel
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException cNFE) {
            System.out.println("Class Not Found Exception: " + cNFE);
        } catch (InstantiationException instE) {
            System.out.println("Instantiate Exception: " + instE);
        } catch (IllegalAccessException iAE) {
            System.out.println("Illegal Access Exception: " + iAE);
        } catch (UnsupportedLookAndFeelException uLAFE) {
            System.out.println("Unreported Look And Feel Exception: " + uLAFE);
        }
		
		setTitle("Laipa ChatBox");
		setIconImage(Toolkit.getDefaultToolkit().getImage("E:\\Documents and Settings\\James' Documents\\net-work\\new\\images\\favicon.png"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 465, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblChatHistory = new JLabel("Chat History");
		lblChatHistory.setFont(new Font("Tahoma", Font.BOLD, 11));
		lblChatHistory.setHorizontalAlignment(SwingConstants.TRAILING);
		
		//JScrollPane scrollPane = new JScrollPane(chatArea);
		
		
		JLabel lblChatBox = new JLabel("Chat Box");
		lblChatBox.setHorizontalAlignment(SwingConstants.LEFT);
		lblChatBox.setFont(new Font("Tahoma", Font.BOLD, 11));
		
		JButton btnEnterButton = new JButton("Enter");
		btnEnterButton.setPreferredSize(new Dimension(60, 30));
		
		JLabel lblTitle = new JLabel("LAIPA ChatBox | Learning Artificially Intelligent Personal Assistant");
		lblTitle.setFont(new Font("Tahoma", Font.BOLD, 12));
		
		JPanel sidePanel = new JPanel();
		sidePanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		
		final JScrollPane scrollPane = new JScrollPane();
		scrollPane.setAutoscrolls(true);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		
		final JTextArea chatBox = new JTextArea();
		chatBox.setFocusCycleRoot(true);
		chatBox.setAutoscrolls(false);
		chatBox.setLineWrap(true);
		chatBox.setWrapStyleWord(true);
		chatBox.setAlignmentX(Component.LEFT_ALIGNMENT);
		chatBox.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		chatBox.setRows(1);
		chatBox.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		chatBox.setFont(new Font("Calibri", Font.PLAIN, 14));
		chatBox.setForeground(new Color(0, 0, 128));
		chatBox.setMinimumSize(new Dimension(4, 40));
		
		
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblChatBox)
						.addComponent(lblTitle)
						.addComponent(lblChatHistory)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
								.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
								.addComponent(chatBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(sidePanel, GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGap(19)
									.addComponent(btnEnterButton, GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)))))
					.addContainerGap())
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(lblTitle)
					.addPreferredGap(ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
					.addComponent(lblChatHistory)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(sidePanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblChatBox, GroupLayout.PREFERRED_SIZE, 13, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(chatBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(btnEnterButton, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		
		
		
		final JTextArea chatArea = new JTextArea();
		chatArea.setEditable(false);
		scrollPane.setViewportView(chatArea);
		chatArea.setMaximumSize(new Dimension(2147483647, 440));
		chatArea.setMinimumSize(new Dimension(4, 435));
		chatArea.setPreferredSize(new Dimension(4, 100));
		chatArea.setRows(24);
		chatArea.setLineWrap(true);
		chatArea.setForeground(new Color(139, 0, 0));
		chatArea.setFont(new Font("Calibri", Font.PLAIN, 14));
		chatArea.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		contentPane.setLayout(gl_contentPane);
		
		btnEnterButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				String inputText = chatBox.getText();
				String existingText = chatArea.getText();
				existingText += inputText + "\n";
				chatArea.setText(existingText);
				
				int number = chatArea.getRows();
				int count = chatArea.getLineCount();
				if(count >= number) {
					chatArea.setRows(chatArea.getRows()+1);
					chatArea.revalidate();
					scrollPane.revalidate();
				}
			}
		});
		
		
		
		
		
	}
}
